/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	MaterialElementInfo.cpp
*
*	Comments	-	See MaterialElementInfo.h
*
**************************************************************************************/
#include "../Include/MaterialElementInfo.h"

namespace Pulse
{
	const CHAR * MaterialElementInfo::GetElementName( EParameter::Type type )
	{
		switch( type )
		{
		case EParameter::VECTOR3:
			return PSX_String("Vector3");
		case EParameter::VECTOR4:
			return PSX_String("Vector4");
		case EParameter::MATRIX:
			return PSX_String("Matrix");
		case EParameter::MATRIX_ARRAY:
			return PSX_String("MatrixArray");
		case EParameter::TEXTURE2D:
			return PSX_String("Texture2D");
		case EParameter::TEXTURE3D:
			return PSX_String("Texture3D");
		case EParameter::UNKNOWN:
			return PSX_String("Unknown");
		}
			
		PSX_PushError( "Not yet implemented" );
		return PSX_NULL;
	}

	EParameter::Type MaterialElementInfo::GetElementType( const CHAR *pType )
	{
		if ( PSX_StrCmp( pType, PSX_String("Vector3") ) == 0 )
			return EParameter::VECTOR3;
		else if ( PSX_StrCmp( pType, PSX_String("Vector4") ) == 0 )
			return EParameter::VECTOR4;
		else if ( PSX_StrCmp( pType, PSX_String("Matrix") ) == 0 )
			return EParameter::MATRIX;
		else if ( PSX_StrCmp( pType, PSX_String("MatrixArray") ) == 0 )
			return EParameter::MATRIX_ARRAY;
		else if ( PSX_StrCmp( pType, PSX_String("Texture2D") ) == 0 )
			return EParameter::TEXTURE2D;
		else if ( PSX_StrCmp( pType, PSX_String("Texture3D") ) == 0 )
			return EParameter::TEXTURE3D;
		else if ( PSX_StrCmp( pType, PSX_String("Unknown") ) == 0 )
			return EParameter::UNKNOWN;

		PSX_PushError( "Not yet implemented." );
		return EParameter::UNKNOWN;
	}

	EErrorCode::Type MaterialElementInfo::GetRequirementElement( XMLElementPtr pElem, MaterialElementRequirement *pRequirement )
	{
		if( pElem.IsNull() || pRequirement == PSX_NULL )
			return EErrorCode::_ERROR;

		pRequirement->m_source = EMaterialSource::UNKNOWN;
		pRequirement->m_type = EParameter::UNKNOWN;

		// Get source, type, then name.
		String buffer = pElem->FindChild( PSX_String("Source") )->GetValue();

		if ( buffer == PSX_String("System") )
			pRequirement->m_source = EMaterialSource::SYSTEM;
		else if ( buffer == PSX_String("User") )
			pRequirement->m_source = EMaterialSource::USER;

		EParameter::Type type = GetElementType( pElem->FindChild( PSX_String("Type") )->GetValue().GetBuffer() );

		if ( type != EParameter::UNKNOWN )
			pRequirement->m_type = type;

		pRequirement->m_name = pElem->FindChild( PSX_String("Name") )->GetValue();

		return EErrorCode::OKAY;
	}

	EErrorCode::Type MaterialElementInfo::GetEntryElement( XMLElementPtr pElem, MaterialElementEntry *pEntry )
	{
		if ( pElem.IsNull() || pEntry == PSX_NULL )
			return EErrorCode::_ERROR;
		
		pEntry->m_type = GetElementType( pElem->FindChild(PSX_String("Type"))->GetValue().GetBuffer() );
		pEntry->m_name = pElem->FindChild(PSX_String("Name"))->GetValue();
		pEntry->m_valueString = pElem->FindChild(PSX_String("Value"))->GetValue();

		if ( pEntry->m_type == EParameter::UNKNOWN)
		{
			pEntry->m_type = EParameter::UNKNOWN;
			pEntry->m_name.Clear();
			pEntry->m_valueString.Clear();
			return EErrorCode::_ERROR;
		}

		return EErrorCode::OKAY;
	}
}